﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace sqlsearcher.Models
{
    public class TextColor
    {
        public static string[] chars = new string[] { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" };
        public static string[] chars2 = new string[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "null" };
        public static string[] chars3 = new string[] { "[", "]", "{", "}" };
        public static void SetColor(RichTextBox txt)
        {
            foreach (var item in chars)
            {
                SetColor(txt, item.ToLower(), Color.Red);
                SetColor(txt, item.ToUpper(), Color.Red);
            }
            foreach (var item in chars2)
            {
                SetColor(txt, item, Color.Blue);
            }
            foreach (var item in chars3)
            {
                SetColor(txt, item, Color.Blue, true);
            }
        }
        private static void SetColor(RichTextBox txt, string key, Color color,bool isBold=false)
        {
            ArrayList list = getIndexArray(txt.Text, key);
            for (int i = 0; i < list.Count; i++)
            {
                int index = (int)list[i];
                txt.Select(index, key.Length);
                txt.SelectionColor = color;
            }

        }
        private static ArrayList getIndexArray(String inputStr, String findStr)
        {
            ArrayList list = new ArrayList();
            int start = 0;
            while (start < inputStr.Length)
            {
                int index = inputStr.IndexOf(findStr, start);
                if (index >= 0)
                {
                    list.Add(index);
                    start = index + findStr.Length;
                }
                else
                {
                    break;
                }
            }
            return list;

        }
    }
}
